import {AgentAdd, AgentVM, getAgent, postAgent, putAgent} from '@/base/api/AgentApi';
import {Agent} from '@/base/model/Agent';

export class FormBloc {
  form = new Agent();

  load(id: number) {
    return getAgent(id)
      .then((res) => {
        this.form = res.data;
        this.form.roles = res.data.roles.map(x => Number(x)) || [];
        return res;
      });
  }

  save(id: number | '') {
    if (id) { // 修改
      const entity: AgentVM = {
        address: this.form.address,
        agentName: this.form.agentName,
        linkName: this.form.linkName,
        phone: this.form.phone,
        roles: [1519321654231072]
      };
       return putAgent(id, entity);
    } else { // 新增
      const entity: AgentAdd = {
        address: this.form.address,
        agentName: this.form.agentName,
        linkName: this.form.linkName,
        phone: this.form.phone,
        password: this.form.password,
        roles: [1519321654231072]
      };
      return postAgent(entity);
    }
  }
}
